/*
 * RtpMediaSource.h
 *
 *  Created on: 2019-04-06
 *      Author: chuanjiang.zh
 */

#ifndef RTPMEDIASOURCE_H_
#define RTPMEDIASOURCE_H_

#include "MediaSource.h"
#include "MediaSourceT.h"

#include "RtpMediaChannel.h"
#include "MediaPacketQueue.h"
#include "RtpMedium.h"
#include "RtpType.h"


namespace av
{


class RtpMediaSource : public MediaSourceT<MediaSource>, public MediaSink
{
public:
    RtpMediaSource();
    virtual ~RtpMediaSource();

    virtual int open(const std::string& url, const std::string& params);

    virtual void close();

    virtual bool isOpen();

    virtual int play();
    virtual int pause();
    virtual void stop();

    virtual int64_t getTime();

    virtual int read(AVPacket& pkt);

    virtual void interrupt();


protected:
    virtual void onMediaFormat(const MediaFormat& fmt);
    virtual void onMediaPacket(MediaPacketPtr& pkt);
    virtual void onMediaEvent(int event);


protected:
    bool parseUrl(const std::string& url);

    bool buildFormat();

    void convertUrlBase64(std::string& str);

    void decodeBase64(const std::string& str, std::string& out);

protected:
    av::RtpMediaChannelPtr  m_videoChannel;
    av::RtpMediaChannelPtr  m_audioChannel;
    MediaPacketQueue    m_pktQueue;
    
    RtpMedium   m_videoMedium;
    RtpMedium   m_audioMedium;

    MediaSinkPtr    m_mediaSink;
    av::RtpProtocol m_protocol;

};


}
#endif /* RTPMEDIASOURCE_H_ */
